require 'net/http'

class StockDailyDetailsController < ApplicationController
  # GET /stock_daily_details
  # GET /stock_daily_details.xml
  def index
    @stock_daily_details = StockDailyDetail.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @stock_daily_details }
    end
  end

  # GET /stock_daily_details/1
  # GET /stock_daily_details/1.xml
  def show
    @stock_daily_detail = StockDailyDetail.find(params[:id])
    @stock_daily_detail.count_daily_detail
    respond_to do |format|
      format.html # show.html.erb
      format.xml { render :xml => @stock_daily_detail }
    end
  end

  # GET /stock_daily_details/new
  # GET /stock_daily_details/new.xml
  def new
    @stock_daily_detail = StockDailyDetail.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @stock_daily_detail }
    end
  end

  # GET /stock_daily_details/1/edit
  def edit
    @stock_daily_detail = StockDailyDetail.find(params[:id])
  end

  # POST /stock_daily_details
  # POST /stock_daily_details.xml
  def create
    header = {
        #'Accept-Encoding'=>'gzip, deflate'
    }
    Net::HTTP::Proxy("127.0.0.1", 8080).start('market.finance.sina.com.cn') { |http|
      response = http.get("/downxls.php?date=#{params[:stock_daily_detail][:deal_day]}&symbol=#{params[:stock_daily_detail][:stock_id]}", header)
      @stock_daily_detail = StockDailyDetail.new(:stock_id => params[:stock_daily_detail][:stock_id], :deal_day =>params[:stock_daily_detail][:deal_day], :daily_detail => response.body.to_s.gsub(/\t/, ","))
    }

    respond_to do |format|
      if @stock_daily_detail.save
        format.html { redirect_to(@stock_daily_detail, :notice => 'Stock daily detail was successfully created.') }
        format.xml { render :xml => @stock_daily_detail, :status => :created, :location => @stock_daily_detail }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @stock_daily_detail.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stock_daily_details/1
  # PUT /stock_daily_details/1.xml
  def update
    @stock_daily_detail = StockDailyDetail.find(params[:id])

    respond_to do |format|
      if @stock_daily_detail.update_attributes(params[:stock_daily_detail])
        format.html { redirect_to(@stock_daily_detail, :notice => 'Stock daily detail was successfully updated.') }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @stock_daily_detail.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stock_daily_details/1
  # DELETE /stock_daily_details/1.xml
  def destroy
    @stock_daily_detail = StockDailyDetail.find(params[:id])
    @stock_daily_detail.destroy

    respond_to do |format|
      format.html { redirect_to(stock_daily_details_url) }
      format.xml { head :ok }
    end
  end
end
